/**
 * Editor of the system
 * 
 * interface package
 * 
 * @author Kelemen Viktor kelemen.viktor@gmail.com
 * @version 0.1
 */
 
YAHOO.namespace('interface');

	/**
	 * Creates the editor
	 *
	 */
	YAHOO.interface.createEditor = function()	{
		
			var tab = "    ";
			function checkTab(evt) {
		
				var t = evt.target;
				var ss = t.selectionStart;
				var se = t.selectionEnd;
				
				if ((evt.ctrlKey)&&(evt.keyCode==13)) { 
					if ($('livejs_editor').value!=null)	{
						var code = ss!=se?$('livejs_editor').value.substring(ss,se):$('livejs_editor').value;
						try {
							eval(code);
						} catch (e) { debug(e); }
					}
				}
				
				
				// Tab key - insert tab expansion
				if (evt.keyCode == 9) {
					evt.preventDefault();
						// Special case of multi line selection
					if (ss != se && t.value.slice(ss,se).indexOf("\n") != -1) {
							var pre = t.value.slice(0,ss);
							var sel = t.value.slice(ss,se).replace(/\n/g,"\n"+tab);
							var post = t.value.slice(se,t.value.length);
							t.value = pre.concat(tab).concat(sel).concat(post);
							t.selectionStart = ss + tab.length;
							t.selectionEnd = se + tab.length;
						}
						// "Normal" case (no selection or selection on one line only)
					else {
						t.value = t.value.slice(0,ss).concat(tab).concat(t.value.slice(ss,t.value.length));
						if (ss == se) {
								t.selectionStart = t.selectionEnd = ss + tab.length;
							}
							else {
								t.selectionStart = ss + tab.length;
								t.selectionEnd = se + tab.length;
							}
					}
				}
				// Backspace key - delete preceding tab expansion, if exists
				else if (evt.keyCode==8 && t.value.slice(ss - 4,ss) == tab) {
					evt.preventDefault();
					t.value = t.value.slice(0,ss - 4).concat(t.value.slice(ss,t.value.length));
					t.selectionStart = t.selectionEnd = ss - tab.length;
				}
				// Delete key - delete following tab expansion, if exists
				else if (evt.keyCode==46 && t.value.slice(se,se + 4) == tab) {
					evt.preventDefault();
					t.value = t.value.slice(0,ss).concat(t.value.slice(ss + 4,t.value.length));
					t.selectionStart = t.selectionEnd = ss;
				}
				// Left/right arrow keys - move across the tab in one go
				else if (evt.keyCode == 37 && t.value.slice(ss - 4,ss) == tab) {
					evt.preventDefault();
					t.selectionStart = t.selectionEnd = ss - 4;
				}
				else if (evt.keyCode == 39 && t.value.slice(ss,ss + 4) == tab) {
					evt.preventDefault();
					t.selectionStart = t.selectionEnd = ss + 4;
				}
			}
		
			var editor = document.createElement('textarea');
			editor.setAttribute('id','livejs_editor');

			var logwindow = document.createElement('textarea');
			logwindow.setAttribute('id','livejs_log');
			logwindow.setAttribute('readonly','readonly');			
			
			YAHOO.util.Dom.addClass(editor,"javascript");
			YAHOO.util.Dom.addClass(editor,"codepress");
			YAHOO.util.Dom.addClass(editor,"linenumbers-off");	
			
			YAHOO.util.Event.addListener(editor,'keypress', checkTab );
			
			// FIXME
			var evalbutton = document.createElement('button');
			evalbutton.setAttribute('id','livejs_evalbutton');
			evalbutton.innerHTML="run";
			YAHOO.util.Event.addListener(evalbutton,'click', function() { 
					if ($('livejs_editor').value!=null)	{
						var code = $('livejs_editor').value;
						try {
							eval(code);
						} catch (e) { debug(e); }
					}
			} );
			
			document.body.appendChild(editor);
			document.body.appendChild(evalbutton);
			document.body.appendChild(logwindow);
			
			// rewrite the log
			log = function(a) { try { console.log(a); logwindow.value=a+'\n'+logwindow.value; } catch(e) { } };
			debug = function(a) { try { console.debug(a); logwindow.value=a+'\n'+logwindow.value; } catch(e) { } };	
	}
	
 
 
